﻿$(document).ready(function () {




    $("#lnkbtnLogOut").click(function () {
        $.post("/Ajax/forumAjax.asmx/LogOut", function () {
            location.reload();
        });
        return false;
    });



    $("#lnkbtnRegister").click(function () {
        open($("#registrationPopUp"));
        $("#txtRegistrationUserName").val("");
        $("#txtRegistrationPassWord").val("");
        return false;
    });

    $("#registrationPopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Registration",
        buttons: {
            "ok": function () {

                var userName = $("#txtRegistrationUserName").val();
                var pass = $("#txtRegistrationPassWord").val();

                if (userName != "" && pass != "") {
                    var parameters = "{'Username':'" + userName + "','Password': '" + pass + "'}";
                    $.ajax({
                        type: "POST",
                        url: "../Ajax/forumAjax.asmx/registration",
                        contentType: "application/json; charset=utf-8",
                        data: parameters,
                        dataType: "json",
                        success: function (result) {

                            if (result.d != "bad") {

                                location.reload();
                                return false;
                            }
                            else {
                                alert("user name is not free");
                                return false;
                            }
                        },
                        error: function () {
                        }
                    });



                }
                else {
                    alert("Must input password and user name");
                    return false;
                }




                $(this).dialog("close");
                return false;
            },
            "cancel": function () {
                $(this).dialog("close");
            }
        }
    });


    $("#lnkbtnLogin").click(function () {
        open($("#loginPopUp"));
        return false;
    });





    $("#loginPopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Login",
        buttons: {
            "ok": function () {

                var userName = $("#txtLoginUserName").val();
                var pass = $("#txtLoginPassWord").val();

                if (userName != "" && pass != "") {
                    var parameters = "{'Username':'" + userName + "','Password': '" + pass + "'}";
                    $.ajax({
                        type: "POST",
                        url: "../Ajax/forumAjax.asmx/Login",
                        contentType: "application/json; charset=utf-8",
                        data: parameters,
                        dataType: "json",
                        success: function (result) {

                            if (result.d != "bad") {

                                location.reload();
                                return false;
                            }
                            else {
                                alert("user name and password are wrong!");
                                return false;
                            }
                        },
                        error: function () {
                        }
                    });



                }
                else {
                    alert("Must input password and user name");
                    return false;
                }




                $(this).dialog("close");
                return false;
            },
            "cancel": function () {
                $(this).dialog("close");
            }
        }
    });




    $(".serachButton").click(function () {
        open($("#searchPopUp"));
        return false;
    });

    $("#lnkbtnSearchByName").click(function () {
        open($("#searchByUserNamePopUp"));
        return false;
    });

    $("#searchByUserNamePopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Search",
        buttons: {
            "Serach by user name": function () {
                $(this).dialog("close");
                return false;
            },
            "Cancel": function () {
                $(this).dialog("close");
                return false;
            }
        }
    });

    $("#ddlSerachByUser").change(function () {
        var userName = $("#ddlSerachByUser option:selected").text();
        window.location.href = 'SearchResults.aspx?userName=' + userName;
        return false;
    });


    $("#searchPopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Search",
        buttons: {
            "Serach": function () {
                $(this).dialog("close");
                return false;
            },
            "Cancel": function () {
                $(this).dialog("close");
                return false;
            }
        }
    });

    $(".Active :checkbox").live("click", function () {
        $.post("../Ajax/forumAjax.asmx/activeUser", { srak: 'srak' });

    });





    $("#lnkbtnSearchByText").click(function () {
        open($("#searchByTextPopUp"));
        return false;
    });

    $("#searchByTextPopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Search by text",
        buttons: {
            "Search": function () {
                var txtQuery = $("#txtToSearchByText").val();
                if (txtQuery != "") {
                    window.location.href = 'SearchResults.aspx?Query=' + txtQuery;
                    return false;
                    $(this).dialog("close");
                    return false;
                }
                else {
                    alert("please inser text");
                    return false;
                }
            },
            "Cancel": function () {
                $(this).dialog("close");
                return false;
            }
        }
    });



    $("#ddlCanBeBannedForum").change(function () {
        parameters = "{'subForumId':'" + $("#ddlCanBeBannedForum option:selected").val() + "'}";
        $.ajax({
            type: "POST",
            url: "../Ajax/forumAjax.asmx/getBannedUsersBySubForum",
            contentType: "application/json; charset=utf-8",
            data: parameters,
            dataType: "json",
            success: function (result) {
                $("#ddlCanBeBannedUsers").html(result.d);
            },
            error: function () {
                return false;
            }
        });
    });

    $("#lnkbtnBannedUsers").click(function () {

        open($("#banUSersPopUp"));
        return false;
    });

    $("#banUSersPopUp").dialog({
        autoOpen: false,
        modal: true,
        title: "Ban users",
        buttons: {
            "ok": function () {
                var forumId = $("#ddlCanBeBannedForum").val();
                var userId = $("#ddlCanBeBannedUsers").val();
                if ($("#ddlCanBeBannedUsers").val() != 0 && $("#ddlCanBeBannedForum").val()) {

                    var parameters;
                    var parameters = "{'forumId':'" + forumId + "','userId': '" + userId + "'}";
                    $.ajax({
                        type: "POST",
                        url: "../Ajax/forumAjax.asmx/banUser",
                        contentType: "application/json; charset=utf-8",
                        data: parameters,
                        success: function (result) {
                            if (result.d != "bad") {
                                location.reload();
                                return false;
                            }
                        },
                        error: function () {
                            return false;
                        }
                    });
                }
                return false;
                $(this).dialog("close");
                return false;
            },
            "Cancel": function () {
                $(this).dialog("close");
                return false;
            }
        }
    });


    $("#LinkButton1").click(function () {


        var x = getParameterByName('ThreadId');
        alert(x=='');
        return false;
    });


    function showUnreadThreads() {
        var parameters = "{'srak':'" + 'srak' + "'}";
        $.ajax({
            type: "POST",
            url: "../Ajax/forumAjax.asmx/getUnreadThreads",
            contentType: "application/json; charset=utf-8",
            data: parameters,
            dataType: "json",
            success: function (result) {
                if (result.d != "bad") {

                    var newFeeds = result.d.split('#');
                    for (var i = 0; i < newFeeds.length - 1; i++) {
                        var threadId = newFeeds[i].split(',')[0];
                        var threadTitle = newFeeds[i].split(',')[1];
                        var threadContent = newFeeds[i].split(',')[2];
                        var subForumId = newFeeds[i].split(',')[3];
                        if (threadTitle != "")
                            $('#unreadThreadList').append('<li> <a class="lnkUnredThreads" href="Thread.aspx?ThreadId=' + threadId + '">' + threadTitle + '</a></li>');
                        else
                            $('#unreadThreadList').append('<li> <a class="lnkUnredThreads" href="Thread.aspx?ThreadId=' + threadId + '">' + threadContent + '</a></li>');
                    }
                }
                else {
                }
            },
            error: function () {
            }

        });
        return false;
    }

    showUnreadThreads();


    //Definition of the function (non-global, because of the previous line)
    function checkUpdates() {
        if ($(".Active :checkbox").attr('checked')) {
            var parameters = "{'srak':'" + 'srak' + "'}";
            $.ajax({
                type: "POST",
                url: "../Ajax/forumAjax.asmx/checkForThreadsUpdates",
                contentType: "application/json; charset=utf-8",
                data: parameters,
                dataType: "json",
                success: function (result) {
                    if (result.d != "bad") {
                        var newFeed = result.d;
                        var newFeedSubForumId = newFeed.split(',')[0];
                        var newFeedId = newFeed.split(',')[1];
                        var newFeedTitle = newFeed.split(',')[2];
                        $('#unreadThreadList').append('<li> <a class="UnreadThreads" href="Thread.aspx?ThreadId=' + newFeedId + '">' + newFeedTitle + '</a></li>');
                    }
                    else {
                    }
                },
                error: function () {
                }

            });
            return false;
        }
    }



    //set an interval
    setInterval(checkUpdates, 10000);

    //Call the function
    checkUpdates();


});

function open(var1) {
    var1.dialog("open");
    var dialog = $(".ui-dialog");
    var left = ($(window).width() / 2) - (dialog.width() / 2);
    dialog.css({ top: 150, left: left });
    return false;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}